package com.example.lenovo.demo_a;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import org.apache.http.HttpResponse;
import org.apache.http.ProtocolVersion;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";
    private static final int GET_RESULT = 1001;
    private  String baseUrl="http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo";
    private EditText edtPhone;
    private TextView txtResult;
    private Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        edtPhone=findViewById(R.id.edtPhone);
        txtResult=findViewById(R.id.txt_result);

        handler=new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message message) {
                if (message.what==GET_RESULT){
                    String result= (String) message.obj;
                    txtResult.setText(result);
                }
                return false;
            }
        });


        findViewById(R.id.btn1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        String phone=edtPhone.getText().toString();
                        useHttpGet(phone);
                    }
                }).start();

            }
        });
        findViewById(R.id.btn2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        String phone=edtPhone.getText().toString();
                        useHttpPost(phone);
                    }
                }).start();

            }
        });
    }

    private void useHttpPost(String phone) {
        //String url="http://a2.gdcp.cn/Nav_liangxue.shtml";
        HttpClient httpClient=new DefaultHttpClient();
        HttpPost httpPost=new HttpPost(baseUrl);

        try {
            List<BasicNameValuePair> nameValuePairList=new ArrayList<>();
            nameValuePairList.add(new BasicNameValuePair("mobileCode",phone));
            nameValuePairList.add(new BasicNameValuePair("userID",""));
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairList));

            HttpResponse httpResponse=httpClient.execute(httpPost);
            int code=httpResponse.getStatusLine().getStatusCode();
            ProtocolVersion protocolVersion=httpResponse.getStatusLine().getProtocolVersion();
            String protocol=protocolVersion.getProtocol()+ protocolVersion.getMajor()
                    + protocolVersion.getMinor();
            Log.e(TAG,"code = " + code +" protocol =" + protocol);

            InputStream inputStream=httpResponse.getEntity().getContent();
            BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream));
            StringBuffer stringBuffer=new StringBuffer();
            String line=bufferedReader.readLine();
            while (line!=null){
                stringBuffer.append(line);
                line=bufferedReader.readLine();
            }
            Log.e(TAG,"content=" + stringBuffer);

            //    txtResult.setText(stringBuffer); 导致崩溃 只能发消息
           /* Message message=Message.obtain();
            message.what=GET_RESULT;
            message.obj=stringBuffer.toString();
            handler.sendMessage(message);*/
        } catch (IOException e) {
            e.printStackTrace();
        }

    }


    private void useHttpGet(String phone) {
        String url=baseUrl.concat("?mobileCode=").concat(phone).concat("&userID=");
        HttpClient httpClient=new DefaultHttpClient();
        HttpGet httpGet=new HttpGet(url);

        try {
            HttpResponse httpResponse=httpClient.execute(httpGet);
            int code=httpResponse.getStatusLine().getStatusCode();
            ProtocolVersion protocolVersion=httpResponse.getStatusLine().getProtocolVersion();
            String protocol=protocolVersion.getProtocol()+ protocolVersion.getMajor()
                    + protocolVersion.getMinor();
            Log.e(TAG,"code = " + code +" protocol =" + protocol);

            InputStream inputStream=httpResponse.getEntity().getContent();
            BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream));
            StringBuffer stringBuffer=new StringBuffer();
            String line=bufferedReader.readLine();
            while (line!=null){
                stringBuffer.append(line);
                line=bufferedReader.readLine();
            }
            Log.e(TAG,"content=" + stringBuffer);

            //    txtResult.setText(stringBuffer); 导致崩溃 只能发消息
            Message message=Message.obtain();
            message.what=GET_RESULT;
            message.obj=stringBuffer.toString();
            handler.sendMessage(message);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
